﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using MandalaySoft.Common;
using System.Data.SqlClient;
using System.Data;

namespace SIMS.BL
{
    public class SubjectDB
    {
        MyDatabase _db = new MyDatabase();

        public DataTable GetSubjectList(int SchoolID, out MyError error)
        {
            DataTable _dt = new DataTable("tblSubject");
            SortedList _paramList = new SortedList();
            _paramList.Add("@SchoolID", SchoolID);

            _dt = _db.GetDataTable("sprocSubjectSelectList", CommandType.StoredProcedure, _paramList, out error);
            return _dt;
        }

        public int Delete(int SubjectID, out MyError error)
        {
            SortedList _paramList = new SortedList();
            _paramList.Add("@SubjectID", SubjectID);

            return _db.ExecuteNonSql("sprocSubjectDeleteSingleItem", CommandType.StoredProcedure, _paramList, out error);
        }

        public Subject GetItem(int SubjectID)
        {
            Subject item = new Subject();

            SortedList _paraList = new SortedList();
            _paraList.Add("@SubjectID", SubjectID);

            MyError _error = new MyError();
            SqlDataReader _reader = _db.GetSqlDataReader("sprocSubjectSelectSingleItem", System.Data.CommandType.StoredProcedure, _paraList, out _error);
            item.Error = _error;

            if (_error.ErrorNo == 0 && _reader.Read())
            {
                item = FillData(_reader, item);
            }

            return item;
        }

        public Subject Save(Subject item)
        {
            try
            {
                SortedList _paraList = new SortedList();
                _paraList.Add("@SubjectID", item.SubjectID);
                _paraList.Add("@SchoolID", item.SchoolID);
                _paraList.Add("@SubjectName", item.SubjectName);
                _paraList.Add("@ShortName", item.ShortName);
                _paraList.Add("@IsActive", item.IsActive);
                _paraList.Add("@CreatedBy", item.CreatedBy);
                _paraList.Add("@ModifiedBy", item.ModifiedBy);

                MyError _error = new MyError();
                Int64 _retValue = _db.Execute("sprocSubjectInsertUpdateSingleItem", System.Data.CommandType.StoredProcedure, _paraList, out _error);
                item.Error = _error;

                if (_retValue > 0)
                {
                    item.SubjectID = Convert.ToInt32(_retValue);
                    item.Error.ErrorMessage = "Saved successfully!";
                }
                else
                {
                    if (_error.ErrorNo == 0)
                    {
                        item.Error.ErrorNo = 10002;
                        item.Error.ErrorLocation = "Subject";
                        item.Error.ErrorMessage = "No row affected.";
                    }
                }

            }
            catch (Exception Ex)
            {
                item.Error.ErrorNo = 10001;
                item.Error.ErrorLocation = "Subject";
                item.Error.ErrorMessage = Ex.Message;
                item.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "Subject");
            }

            return item;
        }

        private Subject FillData(IDataReader aRow, Subject item)
        {
            try
            {
                item = new Subject();
                item.SubjectID = aRow.GetInt32(aRow.GetOrdinal("SubjectID"));
                item.SchoolID = aRow.GetInt32(aRow.GetOrdinal("SchoolID"));
                item.SubjectName = aRow.GetString(aRow.GetOrdinal("SubjectName"));

                if (aRow["ShortName"] != DBNull.Value)
                {
                    item.ShortName = aRow.GetString(aRow.GetOrdinal("ShortName"));
                }
                item.IsActive = aRow.GetBoolean(aRow.GetOrdinal("IsActive"));
                item.CreatedBy = aRow.GetInt32(aRow.GetOrdinal("CreatedBy"));
                if (_db.ColumnExists(aRow, "CreatedByName") && aRow["CreatedByName"] != DBNull.Value)
                {
                    item.CreatedByName = aRow.GetString(aRow.GetOrdinal("CreatedByName"));
                }
                item.CreatedDate = aRow.GetDateTime(aRow.GetOrdinal("CreatedDate"));
                item.ModifiedBy = aRow.GetInt32(aRow.GetOrdinal("ModifiedBy"));
                if (_db.ColumnExists(aRow, "ModifiedByName") && aRow["ModifiedByName"] != DBNull.Value)
                {
                    item.ModifiedByName = aRow.GetString(aRow.GetOrdinal("ModifiedByName"));
                }
                item.ModifiedDate = aRow.GetDateTime(aRow.GetOrdinal("ModifiedDate"));
            }
            catch (Exception Ex)
            {
                item.Error.ErrorNo = 10003;
                item.Error.ErrorLocation = "Subject";
                item.Error.ErrorMessage = Ex.Message;
                item.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "Subject");
            }

            return item;
        }
    }
}
